﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.Cms;
using Model.Config;
using BLL.Cms;
using Com.Ompchina.Lib.Draw;
namespace com.lifan.motorcycle.admin.cms
{
    public partial class add_news : AddFilePage, IAddFile
    {
        protected DateTime ExecuteStart = new DateTime(), ExecuteStop = new DateTime();
        protected void Page_Load(object sender, EventArgs e)
        {
            ExecuteStart = new DateTime();
            base.SetCKFinder("news", ckeContent);
            if (!IsPostBack)
            {
                base.InitOnlineStatus(txtOnline, ddlHour, ddlMinute);
                this.ClearFile();
                if (PageAction.Equals("update")) { this.InitSingleInfo(base.GetUpdateID()); }
            }
        }

        #region IAddFile 成员

        public bool CheckFile()
        {
            if (!string.IsNullOrEmpty(fuUploader.GetUploadFilename()))
                return true;
            else if (fuUploader.DefaultFileCtrl.HasFile)
            {
                AllowFormat[] allow = new AllowFormat[] { AllowFormat.Image, AllowFormat.Swf };
                if (!fuUploader.CheckLength(fuUploader.DefaultFileCtrl, 110 * 1024))
                {
                    base.SetMessage(false, "您上传的文件必须小于100M");
                    return false;
                }
                return true;
            }
            return true;
        }

        public void ClearFile()
        {
            fuUploader.ClearUploadFilename();
        }

        #endregion

        #region IAddInfo 成员

        public bool CheckInput()
        {
            if (ddlTerm.SelectedIndex == 0) 
            {
                SetMessage(false, "请选择分类");
                return false;
            }
            else if (string.IsNullOrEmpty(txtTitle.Text.Trim())) 
            {
                SetMessage(false, "请输入标题");
                return false;
            }
            return true;
        }

        public void InitSingleInfo(int id)
        {
            NewsInfo info = new NewsBLL().GetNewsByID(id);
            if (info.NewsID > 0) 
            {
                hfID.Value = info.NewsID.ToString();
                ddlTerm.DefaultSelect = info.NewsType.TermID.ToString();
                txtTitle.Text = info.NewsTitle;
                fuUploader.SetUploadFilename(info.Filepath);
                txtSummary.Text = info.NewsSummary;
                txtLink.Text = Server.HtmlDecode(info.NewsLink);
                ckeContent.Text = Server.HtmlDecode(info.NewsContent);
                cbRecommend.Checked = info.Recommend.Equals("y");
                txtAuth.Text = info.Author;
                cbOnline.Checked = info.IsOnline.Equals("y");
                txtOnline.Text = info.OnlineTime.ToString("yyyy-MM-dd");
                ddlHour.DefaultSelect = info.OnlineTime.ToString("HH");
                ddlMinute.DefaultSelect = info.OnlineTime.ToString("mm");
            }
        }

        #endregion

        #region 页面私有方法
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput() && this.CheckFile()) 
            {
                if (fuUploader.DefaultFileCtrl.HasFile)
                {
                    fuUploader.SaveFile(fuUploader.DefaultFileCtrl
                        , AbsoluteRoot, "uploads/news/n_");
                }
                NewsBLL bll = new NewsBLL();
                if (hfID.Value.Equals("0"))
                {
                    if (bll.InsertNews(int.Parse(ddlTerm.SelectedValue), HtmlEncode(txtTitle), fuUploader.GetUploadFilename(), HtmlEncode(txtSummary)
                        , HtmlEncode(ckeContent.Text.Trim()), HtmlEncode(txtLink), cbRecommend.Checked ? "y" : "n", HtmlEncode(txtAuth)
                        , base.LoginStaff, cbOnline.Checked ? "y" : "n", DateTime.Parse(txtOnline.Text.Trim() + string.Format(" {0}:{1}", ddlHour.SelectedValue, ddlMinute.SelectedValue))))
                    {
                        SetMessage(true, "新闻新增成功");
                        Redirect("list_news.aspx?action=list", 1);
                    }
                    else
                        SetMessage(false, "新闻新增失败");
                }
                else
                {
                    if (bll.UpdateNews(int.Parse(hfID.Value), int.Parse(ddlTerm.SelectedValue), HtmlEncode(txtTitle), fuUploader.GetUploadFilename()
                        , HtmlEncode(txtSummary), HtmlEncode(ckeContent.Text.Trim()), HtmlEncode(txtLink), cbRecommend.Checked ? "y" : "n", HtmlEncode(txtAuth)
                        , base.LoginStaff, cbOnline.Checked ? "y" : "n", DateTime.Parse(txtOnline.Text.Trim() + string.Format(" {0}:{1}", ddlHour.SelectedValue, ddlMinute.SelectedValue))))
                    {
                        SetMessage(true, "修改新增成功");
                        Redirect("list_news.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        SetMessage(false, "新闻新增失败");
                }
            }
        }
        #endregion
        
    }
}